import { _decorator, RigidBody2D, Vec2 } from 'cc';
import { DIRECTION_ENUM, ENEMY_NAME_ENUM, GAME_MODE_ENUM } from '../../Enums';
import { BaseEnemy } from './BaseEnemy';
import DataManager from '../Runtime/DataManager';
const { ccclass } = _decorator;

@ccclass('DumbEnemy')
export class DumbEnemy extends BaseEnemy {
    speed: number = 4
    positionY: number = 20
    positionX: number = 0
    async init(list) {
        super.initialization(
            "DumbEnemy",
            this.positionX,
            this.positionY,
            this.positionX,
            this.positionY,
            ENEMY_NAME_ENUM,
            list
        )
    }
    // 获取怪物的状态信息
    getSendData() {
        if (this.node && this.node.isValid) {
            let { targetX, targetY, direction, state } = this.playManager
            return {
                name: this.node.name,
                x: Math.round(this.node.getPosition().x / this.speed) * this.speed,// 位置
                y: Math.round(this.node.getPosition().y / this.speed) * this.speed,
                direction,
                state,
                lineList:[],
                fly: false,
                running: false,
                avoid: false,
                status: this.status,
                dizzy: this.dizzy,
                speed: this.speed,
                dizzy_direction: this.dizzy_direction,
                callback: this.callback,
                callback_A: this.callback_A,
                callbackAvoid: this.callbackAvoid,
                targetX,
                targetY
            }
        }
    }
    // 强制设置角色的状态
    setSendData(data) {
        if (!data){
return
        } 
        if (this.node && this.node.isValid) {
            if (!data?.x || !data?.y) return
            this.node.setPosition(data.x, data.y)
            this.playManager.targetX = data.targetX
            this.playManager.targetY = data.targetY
            this.playManager.direction = data.direction
            this.dizzy = data.dizzy
            this.dizzy_direction = data.dizzy_direction
            this.status = data.status
            this.speed = data.speed
            clearTimeout(this.callback)
            clearTimeout(this.callback_A)
            if (this.playManager.state != data.state) {
                this.playManager.state = data.state
            }
        }
    }
    update() {
        
        if (DataManager.Instance.gameMode == GAME_MODE_ENUM.ONLINE) {
            if (!DataManager.Instance.game_ready) return
            if (DataManager.Instance.ping_time >= 460) {
                let rb = this.node.getComponent(RigidBody2D)
                rb!.linearVelocity = new Vec2(0, 0)
                return
            }
        }
        if (this.dizzy) {
            this.dizzy_update()
        } else {

            this.enemy_update()
        }
    }
    enemy_update() {
        if (this.dizzy) return
        let rb = this.node.getComponent(RigidBody2D)
        let speed = this.speed
        let lv = rb!.linearVelocity;
        let { x, y } = this.node.getPosition()
        y = Math.round(y / this.speed) * this.speed
        x = Math.round(x / this.speed) * this.speed
        if (this.status == "left") {
            lv.x = -speed;
            lv = new Vec2(lv.x, 0);
        } else if (this.status == "right") {
            lv.x = speed;
            lv = new Vec2(lv.x, 0);
        } else if (this.status == "top") {
            lv.y = +speed;
            lv = new Vec2(0, lv.y);
        } else if (this.status == "bottom") {
            lv.y = -speed;
            lv = new Vec2(0, lv.y);
        } else {
            lv = new Vec2(0, 0);
        }
        let { targetX, targetY, direction } = this.playManager
        if (direction == DIRECTION_ENUM.LEFT || direction == DIRECTION_ENUM.RIGHT) {
            if (targetX != 0) {
                if (x > parseInt(targetX + '')) {
                    lv.x = -speed;
                    lv = new Vec2(lv.x, 0);
                    this.playManager.disabled = true
                } else if (x < parseInt(targetX + '')) {
                    lv.x = +speed;
                    lv = new Vec2(lv.x, 0);
                    this.playManager.disabled = true
                } else {
                    this.node.setPosition(targetX, y)
                    this.playManager.targetX = 0
                    this.playManager.targetY = 0
                    this.playManager.disabled = false
                    this.status = ""
                    lv = new Vec2(0, 0);
                }
            }
        }
        if (direction == DIRECTION_ENUM.TOP || direction == DIRECTION_ENUM.BOTTOM) {
            if (targetY != 0) {
                if (y < parseInt(targetY + '')) {
                    lv.y = +speed;
                    lv = new Vec2(0, lv.y);
                    this.playManager.disabled = true
                } else if (y > parseInt(targetY + '')) {
                    lv.y = -speed;
                    lv = new Vec2(0, lv.y);
                    this.playManager.disabled = true
                } else {
                    this.node.setPosition(x, targetY)
                    this.playManager.targetY = 0
                    this.playManager.targetX = 0
                    this.playManager.disabled = false
                    this.status = ""
                    lv = new Vec2(0, 0);
                }
            }
        }
        rb!.linearVelocity = lv;
        if (this.initialize && !this.dizzy) {
            this.physicsTestAABB()
        }
    }
}


